<%@ Page language="vb" CodeFile="addInvoiceDistancePenalty.aspx.vb" Inherits="addInvoiceDistancePenalty_aspx_vb" %>

<%
    rowNumber = 1
    vehicleID = Request.QueryString("vehicleID")
    If Len(vehicleID) > 0 Then 
        vehicleID = CLng(vehicleID)
    End IF
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        dbOpen()
        RecSet = Conn.Execute("Select v.leaseStartDate, v.consumedDistance, c.countryCurrency, c.country, p.partnerName, cl.client, pr.product, pl.priceListDuration, pl.priceListCategory, pl.price, pl.priceListDistance, (Select Count(ic.vehicleID) From (tblInvoiceContent ic INNER JOIN tblInvoice i ON i.invoiceID = ic.invoiceID) Where ic.vehicleID = v.vehicleID AND i.invoiceAmount > 0) As vehicleCount From (((((tblVehicle v INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblClient cl ON cl.clientID = v.clientID) INNER JOIN tblProduct pr ON pr.productID = v.productID) INNER JOIN tblPriceList pl ON pl.priceListID = v.priceListID) Where v.vehicleID = " & vehicleID & "")
        If RecSet.EOF Then 
            Response.Redirect("searchVehicle.aspx")
        Else
            leaseStartDate = RecSet.Fields("leaseStartDate").Value
            consumedDistance = RecSet.Fields("consumedDistance").Value
            countryCurrency = RecSet.Fields("countryCurrency").Value
            country = RecSet.Fields("country").Value
            partnerName = RecSet.Fields("partnerName").Value
            client = RecSet.Fields("client").Value
            product = RecSet.Fields("product").Value
            priceListDuration = RecSet.Fields("priceListDuration").Value
            priceListCategory = RecSet.Fields("priceListCategory").Value
            price = RecSet.Fields("price").Value
            priceListDistance = RecSet.Fields("priceListDistance").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        leaseEndDate = DateAdd("m", CDbl(priceListDuration), Convert.ToDateTime(leaseStartDate))
        leaseEndDate = DateAdd("m", CDbl("1"), Convert.ToDateTime(leaseEndDate))
        leaseEndDate = Left(leaseEndDate, 8) & "01"
        leaseEndDate = DateAdd("d", CDbl("-1"), Convert.ToDateTime(leaseEndDate))
        Over()
        strJS = ""
        strJS = strJS + "[""consumedDistance"", ""==''""],"
        strJS = strJS + "[""invoiceAmount"", ""==''""],"
        strJS = strJS + "[""uw"", ""==''""],"
        strJS = strJS + "[""paymentTerm"", ""==''""],"
        strJS = strJS + "[""invoiceDateYear"", ""<1""],"
        strJS = strJS + "[""invoiceDateMonth"", ""<1""],"
        strJS = strJS + "[""invoiceDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>CREATE A PENALTY INVOICE FOR METER READING OVERDUE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='addInvoiceDistancePenalty.aspx?do=save&vehicleID=" & vehicleID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Meter reading (km):</td><td align='right'><input type='text' name='consumedDistance' id='consumedDistance' value='" & consumedDistance & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_consumedDistance' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Invoice Amount (" & countryCurrency & "):</td><td align='right'><input type='text' name='invoiceAmount' id='invoiceAmount' value='' size='40' maxlength='50' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_invoiceAmount' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>UW (" & countryCurrency & "):</td><td align='right'><input type='text' name='uw' id='uw' value='' size='40' maxlength='50' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_uw' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment term (days):</td><td align='right'><input type='text' name='paymentTerm' id='paymentTerm' value='10' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_paymentTerm' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceDateYear' id='invoiceDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = year Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateMonth' id='invoiceDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(month) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateDay' id='invoiceDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(day) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_invoiceDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Invoice text:&nbsp; </td></tr>")
        Response.Write("<tr><td colspan='2' align='right'><textarea name='invoiceText' id='invoiceText' style='width: 100%; height: 50px'></textarea></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='3'>")
        Response.Write("<input type='submit' value='Create invoice'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='4'>LEASE INFORMATION<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Country:</td><td>" & country & "</td></tr>")
        Response.Write("<tr><td>Partner:</td><td>" & partnerName & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Client:</td><td>" & client & "</td></tr>")
        Response.Write("<tr><td>Product:</td><td>" & product & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Duration:</td><td>" & vehicleCount & "/" & priceListDuration & " months</td></tr>")
        Response.Write("<tr><td>Lease contract start:</td><td>" & leaseStartDate & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Lease contract end:</td><td>" & leaseEndDate & "</td></tr>")
        Response.Write("<tr><td>Driving distance:</td><td>" & priceListDistance & " km</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Price category:</td><td>" & priceListCategory & " - " & price & " " & countryCurrency & "</td></tr>")
        Response.Write("<tr><td colspan='4'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        consumedDistance = Request.Form("consumedDistance")
        invoiceAmount = Request.Form("invoiceAmount")
        uw = Request.Form("uw")
        paymentTerm = Request.Form("paymentTerm")
        invoiceDateYear = Request.Form("invoiceDateYear")
        invoiceDateMonth = Request.Form("invoiceDateMonth")
        invoiceDateDay = Request.Form("invoiceDateDay")
        invoiceText = Request.Form("invoiceText")
        consumedDistance = CLng(consumedDistance)
        invoiceAmount = CDbl(invoiceAmount)
        uw = CDbl(uw)
        paymentTerm = CLng(paymentTerm)
        invoiceDateYear = CLng(invoiceDateYear)
        invoiceDateMonth = CLng(invoiceDateMonth)
        invoiceDateDay = CLng(invoiceDateDay)
        If CLng(invoiceDateMonth) < 10 Then 
            invoiceDateMonth = "0" & invoiceDateMonth & ""
        End IF
        If CLng(invoiceDateDay) < 10 Then 
            invoiceDateDay = "0" & invoiceDateDay & ""
        End IF
        invoiceDate = invoiceDateYear & "-" & invoiceDateMonth & "-" & invoiceDateDay
        expiryDate = DateAdd("d", CDbl(paymentTerm), Convert.ToDateTime(invoiceDate))
        invoiceClaim = invoiceAmount
        If Len(invoiceText) > 0 Then 
            invoiceText = Trim(invoiceText)
            invoiceText = Replace(invoiceText, """", "&quot;")
            invoiceText = Replace(invoiceText, "'", "&#39;")
            invoiceText = Replace(invoiceText, vbCrLf, "<br>")
        End IF
        Response.Write("vehicleID = " & vehicleID & "<br>")
        Response.Write("invoiceAmount = " & invoiceAmount & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("uw = " & uw & "<br>")
        Response.Write("invoiceDate = " & invoiceDate & "<br>")
        Response.Write("expiryDate = " & expiryDate & "<br>")
        Response.Write("paymentTerm = " & paymentTerm & "<br>")
        Response.Write("invoiceText = " & invoiceText & "<br>")
        dbOpen()
        Conn.Execute("Update tblVehicle Set consumedDistance = " & consumedDistance & " Where vehicleID = " & vehicleID & "")
        dbClose()
        dbOpen()
        Conn.Execute("Insert Into tblInvoiceDistancePenalty (vehicleID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & vehicleID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')")
        dbClose()
        Response.Redirect("editVehicle.aspx?vehicleID=" & vehicleID & "&subDo=%20-%20SAVED")
    End IF
%>
